//
//  ImgVAouthCode.m
//  NtoccShipper
//
//  Created by mac on 2020/10/24.
//  Copyright © 2020 zhuchongjin. All rights reserved.
//

#import "ImgVAouthCode.h"
#import "CJDateTime.h"

@interface ImgVAouthCode()

@property (nonatomic,strong) FlexFrameView *frameView;
@property (strong, nonatomic) CJImageView *imageVCode; // 图片验证码
@property (strong, nonatomic) CJTextField *fieldAouthCode;
@property (nonatomic,copy) OnSureClick sureBlock;
@property (nonatomic,copy) OnCancleClick cancleBlock;
@property (nonatomic,copy) NSString *strPhoneNum;


@end
@implementation ImgVAouthCode


+ (ImgVAouthCode *)shared {
    
    static dispatch_once_t predicate;
    
    static ImgVAouthCode *objc;
    
    dispatch_once(&predicate, ^{
        
        objc = [[ImgVAouthCode alloc] init];
        
    });
    
    return objc;
}

-(instancetype)initWithPhoneNum:(NSString *)strPhoneNum  ClickOnSure:(OnSureClick )sureClick ClickOnCancle:(OnCancleClick )cancleClick{
    
//    if (self = [super init]) {
          
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.backgroundColor = [[UIColor cjBlackColor] colorWithAlphaComponent:0.3];

        CGRect rcFrame = CGRectMake(0, 0, kScreenWidth - 40, 0);

        if (!self.frameView) {
            self.frameView = [[FlexFrameView alloc] initWithFlex:@"ImgVAouthCode" Frame:rcFrame Owner:self];
        }
        self.frameView.flexibleHeight = YES;


        [self.frameView layoutIfNeeded];
        [self addSubview:self.frameView];


        self.frameView.center = self.center;

        [self setFieldLeftView];
        self.strPhoneNum = strPhoneNum;
        
        self.sureBlock = sureClick;
        self.cancleBlock = cancleClick;
//      }

      [self show];
    
      return self;
}

- (void)setFieldLeftView{
    
    self.fieldAouthCode.leftView = [self createLeftView];
    self.fieldAouthCode.leftViewMode = UITextFieldViewModeAlways;
    
}
- (UIView *)createLeftView{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, LW(10), 0)];
}
- (void)touchViewChangeImageCodeAction{
    [self getImgeCode];
}

- (void)getImgeCode{
       [self.imageVCode sd_setImageWithURL:[NSURL URLWithString:CJStringWithFormat(@"%@/ntocc-tps-api/sms/getVerificationCodeImage?phone=%@&time=%@",CJBaseUrl,self.strPhoneNum,[CJDateTime getNowTimeTimestamp])]];
}

/// 展示隐藏
- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
//    _titleLabel.text = _title;
//    _msgLabel.text = _message;
    self.center = [UIApplication sharedApplication].keyWindow.center;
    [self getImgeCode];
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.0;
    }];
}

- (void)hidden {
    
    @weakify(self);
    [UIView animateWithDuration:0.5 animations:^{
        @strongify(self);
        self.alpha = 0.0;
//        self.labAouthTitle.text = @"获取验证码";
//        [self stopTimer];
//        timeOld=60;
//        self.touchViewGetAouthCode.userInteractionEnabled = YES;
        self.fieldAouthCode.text = @"";

    }];
}


/// 取消
- (void)btnTouchViewClickCancleAction{
    [self hidden];
}
//确认
- (void)btnTouchViewClickSureAction{
    
    if ([[CJControl getThreeUnaryValueStr:self.fieldAouthCode.text] isEqualToString:@""]) {
        [CJAlert showStringDismissWithDelay:@"请填写验证码"];
    }else{
        if (self.sureBlock) {
            self.sureBlock(self.fieldAouthCode.text);
            [self hidden];
            
        }
    }
    
    
}



@end
